<?php
	class dbclass{
		private $conn;
		function __construct($host,$dbname,$username,$userpwd){
			try{
				$this->conn=new pdo("mysql:host=".$host.";dbname=".$dbname,$username,$userpwd);
				$this->conn->query("set names utf8");
			}catch(PDOEXCEPTION $e){
				exit("数据库连接失败:".$e->getMessage());
			}
		}
		function getData($sql,$flag=false){
			//执行SQL返回对象类型数据
			$result=$this->conn->query($sql);
			//设置匹配类型 关联数据
			$result->setFetchMode(PDO::FETCH_ASSOC);
			//执行匹配数据
			if($flag){				//getData填写第二个指ture执行一条，
				$rs=$result->fetch(); 		
			}else{			//↑↑执行 一条
				$rs=$result->fetchAll();	
			}				//↑↑执行多条
			//清空对象
			$result=null;
			//返回对象类型数据
			return $rs;  
		}
		function adddata($table,$data=array()){
		    $sqlbegin="insert into ".$table."(";
		    $sqlstr="";
		    $vstr="";
		    foreach($data as $k=>$v){ //$kname  tel  province ……   $v 王胜男 18555555555 内蒙古
		        $sqlstr.=$k.",";   //name,tel,province,
		        $vstr.="'".$v."',"; // '王胜男','18555523252','内蒙古',
		    }
		    $sqlstr=substr($sqlstr, 0,-1);// name,tel,province
		    $vstr=substr($vstr, 0,-1);// '王胜男','18555523252','内蒙古'
		    $sql=$sqlbegin.$sqlstr.") values(".$vstr.")";
		    return $this->execSql($sql);
		}
		function execSql($sql){
			//执行sql 返回影响函数
			return $this->conn->exec($sql);
		}
		function getlastid(){
		    $sql="select last_insert_id() as id";
		    $rs=$this->getData($sql,true);
		    return $rs["id"];
		} 
		function __destruct(){
			$this->conn=null;
		}
	}
	$db=new dbclass("60.31.214.12","wangshengshop","wangshengshop","123456");
?>